package ln.fxspringarchetype;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

@SpringBootApplication
public class App extends Application {
    private static ApplicationContext applicationContext;

    public static void main(String[] args) {
        //加载spring bean容器
        applicationContext = SpringApplication.run(App.class, args);
        launch(App.class, args);
    }

    /**
     * 获取fxml加载器实例
     *
     * @return
     */
    private FXMLLoader createFxmlLoader() {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setControllerFactory(applicationContext::getBean);
        fxmlLoader.setCharset(StandardCharsets.UTF_8);
        fxmlLoader.setLocation(getClass().getResource("view/index.fxml"));
        return fxmlLoader;
    }

    /**
     * 在此定制主窗体的内容与外观，Stage是Window的子类
     *
     * @param stage
     * @throws IOException
     */
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = createFxmlLoader();
        Scene scene = new Scene(fxmlLoader.load());
        stage.setResizable(false);
        stage.setTitle("Java FX 整合 Spring Boot 应用项目");
        stage.setScene(scene);
        //Caspian主题
        //setUserAgentStylesheet(Application.STYLESHEET_CASPIAN);
        //默认主题Modena
        //setUserAgentStylesheet(Application.STYLESHEET_MODENA);
        //启用JMetro主题?
        //new JMetro(scene, Style.LIGHT);
        stage.getIcons().add(createAppIcon());
        stage.show();
    }

    /**
     * 主窗体图标
     *
     * @return
     */
    private Image createAppIcon() {
        return new Image(Objects.requireNonNull(getClass().getResourceAsStream("/icon/app.png")));
    }
}
